﻿using System;
using System.Data;
using DM4.InfoClasses;
using sscore;

namespace DM4
{
    public class DaemonBase : IDaemon
    {
        public DataTable DtHelp;
        //public static object LockLog = new object();       
        protected string Version = "1.0";

        /// <summary>
        /// ModuleCode Example: "SO-AR"	string
        /// </summary>
        protected string ModuleCode = "DMN_BASE";

        protected DaemonStatus Status = DaemonStatus.Stop;

        //protected Log Log;

        /// <summary>
        /// ModuleName Example: "Sex Offenders - [AR]"	string
        /// </summary>
        protected string Description = "DM4 Daemon Base Class";

        protected bool Debug;

        protected IDM4Core Core;

        public object GetModule()
        {
            return new DaemonBase();
        }

        public string GetModuleCode()
        {
            return ModuleCode;
        }

        public void AddToHelp(string name, string value, string description)
        {
            if (DtHelp == null)
            {
                DtHelp = new DataTable { TableName = "help" };

                DtHelp.Columns.Add("Name", typeof(String));
                DtHelp.Columns.Add("Value", typeof(String));
                DtHelp.Columns.Add("Description", typeof(String));
            }

            DataRow dr = DtHelp.NewRow();
            dr["Name"] = name.Trim();
            dr["Value"] = value.Trim();
            dr["Description"] = description.Trim();

            DtHelp.Rows.Add(dr);
        }

        public void AddToHelp(string name, string value)
        {
            AddToHelp(name, value, "");
        }

        public string GetInfo(string parameter)
        {
            if (parameter.ToUpper().Trim() == DmConsts.Description.ToUpper())
                return Description;

            if (parameter.ToUpper().Trim() == DmConsts.ModuleCode.ToUpper())
                return ModuleCode;

            if (parameter.ToUpper().Trim() == DmConsts.Version.ToUpper())
                return Version;

            return "";
        }

        public XMLInfo GetHelp()
        {
            if (DtHelp == null)
            {
                DtHelp = new DataTable { TableName = "help" };

                DtHelp.Columns.Add("Name", typeof(String));
                DtHelp.Columns.Add("Value", typeof(String));
                DtHelp.Columns.Add("Description", typeof(String));
            }
            return (new XMLInfo()).Load(DtHelp);
        }

        public virtual OperationResult Init(IDM4Core iCore, DmTaskParams prm = null)
        {
            Core = iCore;
            
            ////create log file
            //try
            //{
            //    Log = Log.CreateLogWithoutDataset(String.Format("{0}\\{1}.{2}", Core.GetSysParam("LogDir"), Core.GenerateEntityId(ModuleCode), DmConsts.LogFilesExtension),
            //                                      EncodingType.Windows1251, LogMode.Full, false);
            //}
            //catch (Exception ex)
            //{
            //    WriteErrorLog(ex.Message);
            //    return new OperationResult { Result = DM4Consts.ErrorUnknown, Message = ex.Message };
            //}

            return new OperationResult { Result = DM4Consts.Ok };
        }

        public virtual OperationResult Start()
        {
            Status = DaemonStatus.Starting;
            Status = DaemonStatus.Run;
            return new OperationResult { Result = DM4Consts.Ok };
        }

        public virtual OperationResult Stop()
        {
            Status = DaemonStatus.Stopping;
            Status = DaemonStatus.Stop;

            return new OperationResult { Result = DM4Consts.Ok };
        }

        public DaemonStatus GetStatus()
        {
            return Status;
        }

        public bool IsDebug()
        {
            return Debug;
        }

        public virtual OperationResult Execute(string cmdName, object parameters)
        {
            return new OperationResult { Result = DM4Consts.Ok };
        }
        
        public OperationResult WriteErrorLog(string message)
        {
            //if (Log != null)
            //    lock (LockLog)
            //    {
            //        Log.WriteLog(String.Format("{0} {1}", "----Error----", message), MessageType.Error);
            //    }

            //DMN_Log
            Core.WriteLog(logType: LogType.Daemons, message: message, messType: MessageType.Error, modCode: ModuleCode);
            Core.WriteLog(logType: LogType.Error, message: message, messType: MessageType.Error, modCode: ModuleCode);

            return null;
        }

        public OperationResult WriteLog(string message, MessageType type = MessageType.Info)
        {
            //if (Log != null)
            //    lock (LockLog)
            //    {
            //        Log.WriteLog(message, type);
            //    }

            //DMN_Log
            Core.WriteLog(logType: LogType.Daemons, message: message, messType: type, modCode: ModuleCode);

            if (type == MessageType.Error)
                Core.WriteLog(logType: LogType.Error, message: message, messType: type, modCode: ModuleCode);

            return null;
        }
    }
}
